<?php
  
/**
 * variables actions.
 *
 * @package    jobeet
 * @subpackage variables
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class matrixActions extends sfActions
{
  public function executeIndex(sfRequest $request)
  {
    $this->groups = Doctrine::getTable('ParameterGroup')->createQuery('pg')
      ->where('pg.is_primary = 1')
      ->execute(array(), Doctrine::HYDRATE_ARRAY);
  }
  
  public function executeNew(sfRequest $request)
  {
    if(!empty($_POST['group_1']) && !empty($_POST['group_2'])) {
      $this->groups = array(
        1 => intval($_POST['group_1']),
        2 => intval($_POST['group_2'])
      );
      
      if($this->groups[1] != $this->groups[2]) {
        // First parameters
        $this->primary = Doctrine::getTable('Parameter')->createQuery('p')
          ->where('p.group_id = ?')
          ->execute(array($this->groups[1]), Doctrine::HYDRATE_ARRAY);
        
        // Second parameters
        $this->secondary = Doctrine::getTable('Parameter')->createQuery('p')
          ->where('p.group_id = ?')
          ->execute(array($this->groups[2]), Doctrine::HYDRATE_ARRAY);
        
        //echo '<pre>'; print_r($this->secondary); exit;
      } else exit('You MUST select different groups');
    }
  }
  
  public function executeSave(sfRequest $request)
  {
    // Matrix model
    $matrixTable = Doctrine::getTable('Matrix');
    $matrixModel = new Matrix;
    
    // User parameters
    $groups   = explode('_', $request->getParameter('groups'));
    $existing = Doctrine::getTable('Matrix')
      ->findOneByGroup_1AndGroup_2($groups[0], $groups[1]);
    
    if($existing !== false) {
      Doctrine_Query::create()
      ->update('Matrix')
      ->set('matrix', "'".serialize($request->getParameter('extern'))."'")
      ->where('group_1 = ' . $groups[0])
      ->execute();
    } else {
      $matrix = new Matrix;
      
      $matrix->group_1 = $groups[0];
      $matrix->group_2 = $groups[1];
      $matrix->matrix  = serialize($request->getParameter('extern'));
      
      $matrix->save();
    }
    
    header('Location: /backend_dev.php/matrix'); exit;
    
    return sfView::NONE;
  }
}
